package cn.signalpha.modules.social.controller.admin;

import cn.hutool.core.date.DateUtil;
import cn.signalpha.common.cache.ConfigCache;
import cn.signalpha.common.cache.ConfigKeys;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.dto.ConfigDTO;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.com.dto.UserLevelLogDTO;
import cn.signalpha.modules.com.entity.enums.LevelSourceEnum;
import cn.signalpha.modules.com.service.UserLevelLogService;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.social.dto.TopicDTO;
import cn.signalpha.modules.social.entity.Topic;
import cn.signalpha.modules.social.service.TopicService;
import cn.signalpha.modules.social.vo.TopicVO;
import cn.signalpha.modules.social.wrapper.TopicWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SOCIAL_NAME + "/admin/topic")
@Api(value = "论坛主题", tags = "论坛主题")
public class TopicController extends BaseController {

	private final UserService userService;
	private final TopicService topicService;
	private final UserLevelLogService userLevelLogService;

    @GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="userId", dataType = "string", value = "用户id"),
		@ApiImplicitParam(name="userName", dataType = "string", value = "用户昵称"),
		@ApiImplicitParam(name="content", dataType = "string", value = "发表内容"),
		@ApiImplicitParam(name="isShow", dataType = "string", value = "是否显示：0不显示 1显示"),
		@ApiImplicitParam(name="status", dataType = "string", value = "状态：-1不通过 0待审核 1通过"),
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "开始时间：2023-1-1 00:00:00"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "结束时间：2023-1-1 23:59:59"),
	})
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<TopicVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		return R.data(topicService.selectTopicPage(Condition.getPage(query), params));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<TopicVO> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(TopicWrapper.build().entityVO(topicService.getById(id)));
	}

	@PostMapping("/show")
	@ApiOperation(value = "显示隐藏", notes = "")
	public R show(@Valid @RequestBody TopicDTO.IsShowDTO isShowDTO) {
		Topic topic = Topic.builder().isShow(isShowDTO.getIsShow()).build();
		return R.status(topicService.update(topic, Wrappers.<Topic>lambdaUpdate().in(Topic::getId, Func.toStrList(isShowDTO.getId()))));
	}

	@PostMapping("/audit")
	@ApiOperation(value = "审核", notes = "")
	public R audit(@Valid @RequestBody TopicDTO.AuditDTO auditDTO) {
		Date now = new Date();
		Date beginTime = DateUtil.beginOfDay(now);
		ConfigDTO.SocialDTO config = ConfigCache.getObject(ConfigKeys.SOCIAL, ConfigDTO.SocialDTO.class);
		Func.toLongList(auditDTO.getId()).forEach(id -> {
			Topic topic = topicService.getById(id);
			Assert.isNull(topic, MsgConstant.OBJECT_NULL);
			topicService.updateById(Topic.builder().id(topic.getId()).status(auditDTO.getStatus()).remark(auditDTO.getRemark()).build());
			if(auditDTO.getStatus() == 1){
				long count = userLevelLogService.countByParams(Kv.create().set("sourceType", "social").set("createTimeBegin", beginTime).set("createTimeEnd", now));
				if(config.getExpNum() > count){
					userService.updateUserExp(UserLevelLogDTO.builder()
						.userId(topic.getUserId())
						.amount(config.getExp())
						.sourceId(topic.getId().toString())
						.sourceType(LevelSourceEnum.SOCIAL.getType())
						.remark(LevelSourceEnum.SOCIAL.getName())
						.build());
				}
			}
		});
		return R.status(true);
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(topicService.removeByIds(Func.toLongList(ids)));
	}
}
